/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelviewer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.lwjgl.util.vector.Vector3f;

public class OBJLoader
{
	public static Model loadModel(File f) throws FileNotFoundException, IOException
	{
		BufferedReader reader = new BufferedReader(new FileReader(f));
		Model m = new Model();

		String line;
		while ((line = reader.readLine()) != null)
		{
			String[] pieces = line.split(" ");
			if (line.startsWith("v "))
			{
				float x = Float.valueOf(pieces[1]);
				float y = Float.valueOf(pieces[2]);
				float z = Float.valueOf(pieces[3]);
				m.vertices.add(new Vector3f(x, y, z));
			} else if (line.startsWith("vn "))
			{
				float x = Float.valueOf(pieces[1]);
				float y = Float.valueOf(pieces[2]);
				float z = Float.valueOf(pieces[3]);
				m.normals.add(new Vector3f(x, y, z));
			} else if (line.startsWith("f "))
			{
				Vector3f vertexIndices = new Vector3f(Float.valueOf(pieces[1].split("/")[0]), Float.valueOf(pieces[2].split("/")[0]),
						Float.valueOf(pieces[3].split("/")[0]));

				Vector3f normalIndices = new Vector3f(Float.valueOf(pieces[1].split("/")[2]), Float.valueOf(pieces[2].split("/")[2]),
						Float.valueOf(pieces[3].split("/")[2]));
				
				m.faces.add(new Face(vertexIndices, normalIndices));
			}
		}
		reader.close();
		return m;
	}
}
